/*****************************************************************************
 * Copyright (C) Stacy Curl. All rights reserved.                            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *****************************************************************************/
package picounit.features.test_instantiator;

import picounit.util.Equal;
import picounit.verify.constraint.EqualUtil;

import java.lang.reflect.Modifier;

public class ImplementsCondition implements Condition, Equal<ImplementsCondition> {
	private final Class<?> toImplement;

	public ImplementsCondition(Class<?> toImplement) {
		this.toImplement = toImplement;
	}

	public boolean matches(Class<?> aClass) {
		return toImplement.isAssignableFrom(aClass) &&
			!Modifier.isAbstract(aClass.getModifiers());
	}
	
	public boolean hasImplementsClass(Class<?> toImplement) {
		return this.toImplement.equals(toImplement);
	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	// Equal methods
	///////////////////////////////////////////////////////////////////////////////////////////////
	
	public boolean equalTo(ImplementsCondition other) {
		return other.hasImplementsClass(toImplement);
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	// Object methods
	///////////////////////////////////////////////////////////////////////////////////////////////

	@Override
	public boolean equals(Object object) {
		return EqualUtil.isEqual(this, object);
	}

	@Override
	public int hashCode() {
		return toImplement.hashCode();
	}

	@Override
	public String toString() {
		return getClass().getName() + ": " + toImplement;
	}
}
